package com.yumeng.framework.persistence.mybatis.injector.methods;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.yumeng.framework.persistence.mybatis.data.MybatisPlusExtConstants;
import com.yumeng.framework.persistence.mybatis.injector.MyAbstractMethod;
import com.yumeng.framework.persistence.mybatis.injector.MySqlMethod;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.Map;

/**
 * @author wxd
 * @date 2021/12/15 10:15
 */
public class LogicDeleteByMap extends MyAbstractMethod {

    private static final MySqlMethod SQL_METHOD = MySqlMethod.LOGIC_DELETE_BY_MAP;

    public LogicDeleteByMap() {
        super(SQL_METHOD.getMethod());
    }

    /**
     * 注入自定义 MappedStatement
     * "<script>\nUPDATE %s %s %s\n</script>"
     * @param mapperClass mapper 接口
     * @param modelClass  mapper 泛型
     * @param tableInfo   数据库表反射信息
     * @return MappedStatement
     */
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        if (!myHasLogicDelete(tableInfo)) return null;
        String sql = String.format(SQL_METHOD.getSql(), tableInfo.getTableName(), mySqlLogicSet(tableInfo, MybatisPlusExtConstants.COLUMN_MAP_LOGIC_DEL_EXT_DOT), mySqlWhereByMapForLogicDelete(tableInfo));
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Map.class);
        return addUpdateMappedStatement(mapperClass, Map.class, sqlSource);
    }
}
